﻿using System;
using System.Data.SqlClient;

namespace SkyConnector.Model
{
    class CloudConnection
    {
        private readonly SqlConnection mConn;

        public CloudConnection()
        {
            mConn = new SqlConnection("Server=tcp:emikuqiw7t.database.windows.net,1433;Database=GrillMaster;User ID=grillmasteruser@emikuqiw7t;Password=GrillMaster!;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;");
        }

        private string generateSQLInsertStatement(int temperature)
        {
            return @"UPDATE Temperatures 
                    SET temperature='" + temperature.ToString() + "'" +
                   "WHERE ID='1'";
        }

        public string uploadToDatabase(int temperature)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(generateSQLInsertStatement(temperature), mConn);
                
                mConn.Open();
                cmd.ExecuteNonQuery();
                mConn.Close();

                return "Upload completed.";
            }

            catch (Exception)
            {
                return "Upload failed!";
            }
        }
    }
}
